package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.utils.QiniuCloudUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value="获取所有操作员")
    @GetMapping("/system/admin/")
    public List<Admin> getAllAdmins(String keywords){
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value="更新操作员")
    @PutMapping("/system/admin/")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminService.updateById(admin)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value="删除操作员")
    @DeleteMapping("/system/admin/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/system/admin/roles")
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/system/admin/role")
    public RespBean updateAdminRole(Integer adminId,Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }



//    @Value("${fastdfs.nginx.host}")
//    String nginxHost;

    @ApiOperation("修改用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdminInfo(@RequestBody Admin admin){
        adminService.updateAdminInfo(admin);
        return RespBean.success("用户信息修改成功！");
    }

    @ApiOperation(value = "修改用户密码")
    @PutMapping("/admin/pass")
    public RespBean updatePassword(@RequestBody Map<String,Object> ruleForm){
        adminService.updatePassword(ruleForm);
        return RespBean.success("用户密码修改成功！");
    }

    @ApiOperation(value = "修改头像")
    @PostMapping("/admin/userface")
//    public RespBean updateFace(Principal principal, MultipartFile file) throws IOException {
//
//        Integer i = adminService.updateFace(principal, file);
//
//        if (i > 0) {
//            return RespBean.success("修改成功");
//        } else {
//            return RespBean.error("修改失败");
//        }
//
//    }
    public RespBean uploadImg(@RequestParam MultipartFile file, HttpServletRequest request) {

        System.out.println(file);
        RespBean result =new RespBean();
        if (file.isEmpty()) {
           /* result.setCode(400);
            result.setMsg("文件为空，请重新上传");*/
            result.error("文件上传发生异常！");
            return result;
        }

        try {
            byte[] bytes = file.getBytes();
            String imageName = UUID.randomUUID().toString();

            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
               /* result.setCode(200);
                result.setMsg("文件上传成功");
                result.setInfo(url);*/
                System.out.println(url);

                result.success("文件上传成功",url);
                adminService.updatetu(url,request);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {
           /* result.setCode(500);
            result.setMsg("文件上传发生异常！");
            return result;*/
            result.error("文件上传发生异常！");
            return result;
        }

    }

}
